package com.yanqu.road.server.cmd.activity.douluodalu;

import com.yanqu.road.entity.activity.douluodalu.config.DouLuoDaLuConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.douluodalu.DouLuoDaLuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.douluodalu.DouLuoDaLuMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.PropertyHelper;


@Cmd(code = Protocol.S_DOU_LUO_DA_LU_GET_WIN_REWARD,desc = "领取连胜奖励")
public class DouLuoDaLuGetBattleWinRewardCmd extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        if ((ret = DouLuoDaLuMgr.canEntryActivity(player)) != 0) return;

        if ((ret = DouLuoDaLuMgr.activityInDuiJuePeriod()) != 0) return;

        DouLuoDaLuConfig config = DouLuoDaLuMgr.getConfig();

        DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardReqMsg reqMsg = DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardReqMsg.parseFrom(packet.getBytes());
        int winTimes = reqMsg.getWinTimes();
        int boxId = config.getWinRewardBox(winTimes);
        if(boxId == 0){
            ret = GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
            return;
        }
        DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardCrossReqMsg.Builder crossReqMsg = DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardCrossReqMsg.newBuilder();
        //区服随机结果给跨服
        //奖励是宝箱或者百业经验
        GoodsInfo goodsInfo = GoodsMgr.getGoodsById(boxId);
        if(goodsInfo == null){
            ret = GameErrorCode.E_GOODS_NO_EXIST;
            return;
        }
        OpenGoodsBagResult openResult = GoodsBagMgr.getRandomGoods(goodsInfo.getGoodsId());

        if(openResult.getReward().getCountByGoodsId(GameConfig.PATRONS_BAIYE_EXP_ID).intValue() > 0){
            crossReqMsg.setPatronsExp(openResult.getReward().getCountByGoodsId(GameConfig.PATRONS_BAIYE_EXP_ID).intValue());
        }else {
            crossReqMsg.setRewards(PropertyHelper.parsePropertyToString(openResult.getReward()));
        }
        //随机两次奖励给客户端
        for (int i = 0; i < 2; i++) {
            OpenGoodsBagResult tempResult = GoodsBagMgr.getRandomGoods(goodsInfo.getGoodsId());
            crossReqMsg.addOtherRewards(PropertyHelper.parsePropertyToString(tempResult.getReward()));
        }
        crossReqMsg.setBoxId(boxId);
        crossReqMsg.setWinTimes(winTimes);
        crossReqMsg.setActivityId(DouLuoDaLuMgr.getActivityInfo().getActivityId());
        player.sendUnionActivityGroupPacket(Protocol.C_DOU_LUO_DA_LU_GET_WIN_REWARD, crossReqMsg, crossReqMsg.getActivityId());
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            player.sendPacket(Protocol.U_DOU_LUO_DA_LU_GET_WIN_REWARD, DouLuoDaLuProto.DouLuoDaLuGetBattleWinRewardRespMsg.newBuilder().setRet(ret));
        }
    }
}
